import { queryTagDetails } from '@/services/api';
import { useQuery } from '@tanstack/react-query';
import type { ITag } from '@/interfaces';
import { useContext } from 'react';
import Alert from '@/app/[locale]/alert/alert';
import { TagAdminContext } from '@/contexts/admin/forum/tag';
import AlertLoad from '@/app/[locale]/alert/load';
import QueryTagInfo from '@/app/[locale]/admin/forum/tag/detail/info';
import QueryTagTagGroup from '@/app/[locale]/admin/forum/tag/detail/tagGroup';
import QueryTagSections from '@/app/[locale]/admin/forum/tag/detail/sections';
import QueryTagPosts from '@/app/[locale]/admin/forum/tag/detail/posts';

export default function DetailsTagAdminPage() {
  const context = useContext(TagAdminContext);

  const queryTagDetailsQuery = useQuery(
    ['/forum', '/tags', context.detailItem?.id, '/details'],
    async (context) => {
      return (await queryTagDetails({
        id: context.queryKey[2] + '',
      })) as ITag;
    },
  );

  if (queryTagDetailsQuery.data) {
    const data = queryTagDetailsQuery.data;

    return (
      <div className="row">
        <div className="col">
          <div className="vstack gap-4">
            <QueryTagInfo details={data} />

            <QueryTagTagGroup details={data} />

            <div className="row">
              <div className="col">
                <QueryTagSections details={data} />
              </div>
              <div className="col">
                <QueryTagPosts details={data} />
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }

  if (queryTagDetailsQuery.error) {
    return <Alert type="error" message={queryTagDetailsQuery.error} />;
  }

  return <AlertLoad />;
}
